﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Store;

namespace Rodunu.Movies.SearchEngine
{
    public class IndexSearcher
    {
        private IndexFactory _indexFactory;
        private Directory _indexDirectory;

        public IndexSearcher()
        {
            _indexFactory = new IndexFactory();
            _indexDirectory = _indexFactory.IndexDirectory;
        }

        public IList<string> Search(string keywords)
        {
            Lucene.Net.Search.IndexSearcher indexSearcher = new Lucene.Net.Search.IndexSearcher(_indexDirectory);
            QueryParser parser = new QueryParser("MovieName", new StandardAnalyzer());
            Query query = parser.Parse(keywords);
            Hits hits = indexSearcher.Search(query);
            IList<string> movies = new List<string>();
            // Iterate through the results:
            for (int i = 0; i < hits.Length(); i++)
            {
                Document hitDoc = hits.Doc(i);
                movies.Add(hitDoc.Get("MovieName"));
                //assertEquals("This is the text to be indexed.", hitDoc.get("fieldname"));
            }
            indexSearcher.Close();

            return movies;
        }
    }
}
